package com.aiyige.page.my.customer.adapter;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.page.my.customer.callback.ScreenCallback;
import com.aiyige.page.my.customer.model.CustomerScreenChildEntity;
import com.aiyige.utils.ListUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.LinkedList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 右侧列表
 * Created by xiachao
 */

public class CustomerScreenRightAdapter extends BaseQuickAdapter<CustomerScreenChildEntity, BaseViewHolder> {



    public CustomerScreenRightAdapter() {
        super(new LinkedList<CustomerScreenChildEntity>());
    }

    ScreenCallback callback;

    public void setCallback(ScreenCallback callback) {
        this.callback = callback;
    }

    @Override
    protected void convert(BaseViewHolder helper, CustomerScreenChildEntity item) {
        ((MyHolder) helper).bindData(item);
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        BaseViewHolder baseViewHolder = new MyHolder(getItemView(R.layout.item_screen_right_selector, parent));
        return baseViewHolder;
    }



    public class MyHolder extends BaseViewHolder {
        @BindView(R.id.tv_name)
        TextView tv_name;



        public MyHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
        public void bindData(final CustomerScreenChildEntity data) {
            Log.v("xiachaocc",""+data.getName());
            setText(R.id.tv_name,data.getName());
            tv_name.setSelected(data.isSelected());
            tv_name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (callback != null) {
                        if (data.isSelected()) {
                            callback.onRightDelete(data);
                        } else {
                            callback.onRightClick(data);
                        }
                    }
                }
            });

        }
    }

    public synchronized void unSelect(CustomerScreenChildEntity entity) {
        if (!ListUtil.isEmpty(getData())) {
            for (CustomerScreenChildEntity listEntity : getData()) {
                if (listEntity.getId().equals(entity.getId())) {
                    listEntity.setSelected(false);
                    notifyDataSetChanged();
                    break;
                }
            }
        }
    }

}
